using System;
using System.Threading;
using Microsoft.SPOT;
using DaaseVarmer.domain;
using DaaseVarmer.helperClasses;

namespace DaaseVarmer
{
    public class MainLoop
    {

        public MainLoop()
        {

        }

        public void Run()
        {
            while (true)
            {
                lock (Settings.Instance)
                {
                    //state machine if you want to manuelly set speed or minimum temperatur
                    if (Settings.Instance.ManuelOverride)
                    {
                        ManuelSpeed();
                    }
                    else
                    {
                        AutomaticSpeed();
                    }
                }

                Thread.Sleep(Settings.Instance.SampleTime);
            }

        }

        //the manuel mode where the motor will be set to contsant speed
        private void ManuelSpeed()
        {
            Debug.Print("manuel is running");
            // sets motor to motorspeed
            MotorShieldDriver.Instance.MotorStepSpeed = Settings.Instance.MotorSpeed;
        }

        //the solarheater runs automated
        private void AutomaticSpeed()
        {
            Debug.Print("automatic is running");
            if (!Settings.Instance.isRealTime)
            {
                //Debug.Print(" trying to set networktime: ");
                //Debug.Print(" " + Settings.GetNetworkRealTime());
            }
            try
            {

                int soltemp = TemperatureSensorDriver.Instance.GetTemperatur(WriteType.TempSolarHeater);
                var motorControlTemp = soltemp;
                LogManager.Instance.LogData(WriteType.TempSolarHeater, soltemp.ToString());
                Debug.Print(TemperatureSensorDriver.Instance.WriteTypeEnumToString(WriteType.TempSolarHeater) + " : " + soltemp);
                
                int outside = TemperatureSensorDriver.Instance.GetTemperatur(WriteType.TempOutside);
                LogManager.Instance.LogData(WriteType.TempOutside, outside.ToString());
                Debug.Print(TemperatureSensorDriver.Instance.WriteTypeEnumToString(WriteType.TempOutside) + " : " + outside);


                goMotor(soltemp);
                TemperatureSensorDriver.Instance.Dispose();
            }
            catch (Exception e)
            {
                Debug.Print(e.Message + " : " + e.ToString());

            }

        }


        //makes the motor go faster/slower baset om the inout vs minTemp in settings
        private void goMotor(int motorControlTemp)
        {
            if (motorControlTemp > Settings.Instance.MinTemperatureMotorStart)
            {
                if (!MotorShieldDriver.Instance.IsMaxSpeed())
                {
                    Debug.Print("motor go faster");
                    MotorShieldDriver.Instance.MotorGoFaster();
                    Settings.Instance.MotorSpeed = MotorShieldDriver.Instance.MotorStepSpeed;
                }
            }

            if (motorControlTemp < Settings.Instance.MinTemperatureMotorStart)
            {
                if (!MotorShieldDriver.Instance.IsMinSpeed())
                {
                    Debug.Print("motor slower");
                    MotorShieldDriver.Instance.MotorGoSlower();
                    Settings.Instance.MotorSpeed = MotorShieldDriver.Instance.MotorStepSpeed;
                } else
                {
                    MotorShieldDriver.Instance.MotorStepSpeed = Settings.Instance.MotorSpeed;
                }
            }

            LogManager.Instance.LogData(WriteType.MotorSpeed, Settings.Instance.MotorSpeed.ToString());

            Debug.Print("motorSpeed: " + MotorShieldDriver.Instance.MotorStepSpeed);
            Debug.Print("memory available: " + Debug.GC(true));
            Debug.Print("*******************");
            
        }

    }
}
